package com.example.moilapp.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.example.moilapp.Moil_DO;
import com.example.moilapp.adapter.Moil_Pending_Task_Adapter;

public class Moil_Status_Service  extends AsyncTask<String, Void, String> {

	private ProgressDialog progdialod;
	private Activity activity;
	private Context context;
	
	Moil_Pending_Task_Adapter listAdapter;
	
    ExpandableListView expListView;
    List<String> listDataHeader = new ArrayList<String>();
    HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
	
	//private String[] serial_no_arr;
	private String Type;
	private String Status;
	
	String Date;
	String SerialNo;
	
	CheckBox chkbx;
	String shrdPrfval;
	
	private boolean isLast;
	private int position;
	private int pickup_delivery;
	
	// connection establish timeout (timeoutConnection)
	private int timeoutConnection = 5000;
	// socket timeout (timeoutSocket) 
	private int timeoutSocket = 45000;
	
	private static byte[] buff =new byte[1024];
	private String retval="";
	
	private int[] app_ids;
	
	String pass_job_id="";
	
	public static final String PREFS_NAME = "PrefsFile";

	JSONArray jArray;
	private static final String SOAP_ACTION = "http://tempuri.org/JobStatusBySerial";
	
    private static final String METHOD_NAME = "JobStatusBySerial";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://t2a1.cloudapp.net/TrafficWorkflow/TraficWorkflowWebService.asmx";
	private static final String TAG = null;
	
	SharedPreferences settings;
	SharedPreferences.Editor editor;
	
	public Moil_Status_Service(Activity activity,String SerialNo,String Type,String Status,String Date,CheckBox chkbx,String shrdPrfval) {
		super();
		this.activity = activity;
		this.context = this.activity.getApplicationContext();
		//this.serial_no_arr = new String[serialArr.length];
		//this.serial_no_arr = serialArr;
		this.Type = Type;
		this.Status = Status;
		this.SerialNo = SerialNo;
		this.Date = Date;
		this.chkbx = chkbx;
		this.shrdPrfval = shrdPrfval;
		
		settings = this.activity.getSharedPreferences(PREFS_NAME, 0);
		editor = settings.edit();
		
		/*if(Integer.parseInt(de_cypher_PickupDelivery(serial_no_arr[position-1]))==pickup_delivery){
			
		}*/
		Log.e("SerialNo", ""+SerialNo);
		Log.e("Date", ""+Date);
		Log.e("pickup_delivery", ""+Type);
		Log.e("Status", ""+Status);
		//Log.e("pickup_delivery array val", ""+Integer.parseInt(de_cypher_PickupDelivery(serial_no_arr[position-1])));
		
		//Log.e("serial_no_arr[position]", serial_no_arr[position-1]);
	}
	
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progdialod = ProgressDialog.show(this.activity, "Search", "Updating",true,false);
	}
	
	
	@Override
	protected String doInBackground(String... urls) {
		
		/*if((Integer.parseInt(de_cypher_PickupDelivery(serial_no_arr[position-1])))== pickup_delivery){
			*/
		
			try {

	            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

	            /*request.addProperty("SerialNumber", de_cypher_Serial(serial_no_arr[position-1]));
	            request.addProperty("Date",  de_cypher_Date(serial_no_arr[position-1]));
	            */
	            
	            request.addProperty("SerialNumber", SerialNo);
	            request.addProperty("Date", Date);
	            request.addProperty("Type", Type);
	            request.addProperty("Status", Status);

	            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	            envelope.dotNet=true;
	            envelope.setOutputSoapObject(request);

	            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
	            androidHttpTransport.call(SOAP_ACTION, envelope);
	            
	            SoapPrimitive resultSoapPrimitive;
	            resultSoapPrimitive = (SoapPrimitive) envelope.getResponse();
	                
	            String result = resultSoapPrimitive.toString();

	            retval = result;
	            
	            Log.d("return", retval);
	            
	            return retval;
	           
	        } catch (Exception e) {
	            
	       }
			
		
		
		 
		return retval;
		
	}
	
	@Override
	protected void onPostExecute(String result) {
		
		
		
		Log.d("Post Exec", "POST EXEC");
		
		/*Toast toast = Toast.makeText(activity, result, Toast.LENGTH_SHORT);
		toast.show();*/
		
		Log.d("Post tt", result);
		progdialod.dismiss();

		Log.d("Post Exec", result);
		
		//String nwstr = result.replaceAll("\\r\\n\\t", "");
		
		//String newResult = nwstr.replace(" ", "");
		//String newLowRes = newResult.toLowerCase().trim();
		
		Log.e("newResult.equals(RecordAdded)", ""+(result.toString().trim().equals("\"Record Added\"")));
		//Log.e("newResult",newResult);
		//Log.e("newLowRes",newLowRes);
		//Log.e("newLowRes equals",""+(newLowRes=="recordadded"));
		
		Log.e("shrdPrfval", shrdPrfval);
		
		if(result.toString().trim().equals("\"Record Added\"")){
			editor.putBoolean(shrdPrfval,chkbx.isChecked()).commit();
			Toast.makeText(context,"Record Updated To "+Status,Toast.LENGTH_SHORT).show();
		}else{
			chkbx.setChecked(false);
			Toast.makeText(context,"Record Not Updated !!",Toast.LENGTH_SHORT).show();
		}
		
		/*try {
			
			JSONObject element01 = new JSONObject(result);
			String respond = element01.getString("JobStatusResult");

			Log.d("respond", respond);;
		
		} catch (JSONException e) {
			e.printStackTrace();
		}*/
		
		/*if(!isLast){
			Moil_Status_Service mps = new Moil_Status_Service(activity,serial_no_arr,position+1,((serial_no_arr.length-1)==(position))?true:false,"Delivery","Acknowledged",pickup_delivery);
			mps.execute(new String[] {"http://t2a1.cloudapp.net/TrafficWorkflow/TraficWorkflowWebService.asmx"});
		}*/
		
	}
	
	private String de_cypher_Serial(String fullthing){
		
		String[] seperated = fullthing.split("~");
		
		return seperated[1];
	}
	
	private String de_cypher_Date(String fullthing){
		String[] seperated = fullthing.split("~");
		
		return seperated[2];
	}
	private String de_cypher_PickupDelivery(String fullthing){
		String[] seperated = fullthing.split("~");
		
		return seperated[3];
	}

}
